package com.example.administrator.uitest.activity;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.LinearInterpolator;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.uitest.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity {
    private Timer timer,timer1;
    private TextView tv_time,tv_click_num;
    private float controltime = 60;
    private TimerTask timerTask,timerTask1;
    private TextView tv_hit;
    private int click_num = 0;
    private int hit = 0;
    private int maxHit = 0;
    private List<Integer> allHit = new ArrayList();
    private boolean isHit = true;
    private int WindowHeight = 0;
   private RelativeLayout re1,re2,re3,re4;
   private int leveltime=500,leveldown = 2500;
   private int nums = 0;

    @Override
    public void onBackPressed() {
        Log.e("txh","back");
        finish();
        if(timer!=null){
            timer.cancel();
            timer.purge();
            timer = null;
        }
      if(timer1!=null){
          timer1.cancel();
          timer1.purge();
          timer1 = null;


      }
      startActivity(new Intent(MainActivity.this,HomeActivity.class));

    }
    Handler handler1 = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            controltime = controltime-1;
            if(controltime == 0){
                timer.cancel();
                timer.purge();
                timer = null;
                timer1.cancel();
                timer1.purge();
                timer1 = null;
                //结束
                Intent intent = new Intent(MainActivity.this,Finish_Activity.class);
                intent.putExtra("num",click_num);
                if (isHit){
                    maxHit = hit;
                }else {
                    maxHit = allHit.get(0);
                    for(int i=0;i<allHit.size();i++){
                        if(allHit.get(i)>maxHit){
                            maxHit = allHit.get(i);
                        }
                    }
                }
                intent.putExtra("hit",maxHit);
                startActivity(intent);
                finish();
            }else {
                tv_time.setText("" + Integer.valueOf((int) controltime));
            }
        }
    };
    Handler handler = new Handler(){
       @Override
       public void handleMessage(Message msg) {
           super.handleMessage(msg);
               addNewView();
       }
   };

    private void addNewView() {
        int num = (int) (Math.random()*4);
        final View mode = LayoutInflater.from(MainActivity.this).inflate(R.layout.mode,null);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,400);
        mode.setLayoutParams(params);
        switch (num){
            case 0:re1.addView(mode);break;
            case 1:re2.addView(mode);break;
            case 2:re3.addView(mode);break;
            case 3:re4.addView(mode);break;
        }
        final ObjectAnimator trans = ObjectAnimator.ofFloat(mode,"translationY",-400,WindowHeight)
                .setDuration(leveldown);
        trans.setInterpolator(new LinearInterpolator());
        trans.start();

        mode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mode.setBackgroundResource(R.drawable.mode_bc);
                mode.setOnClickListener(null);
                click_num++;
                hit++;
                tv_hit.setText(""+hit);
                tv_click_num.setText(""+click_num);
               trans.removeAllListeners();
            }
        });
       trans.addListener(new ObjectAnimator.AnimatorListener() {
           @Override
           public void onAnimationStart(Animator animator) {

           }

           @Override
           public void onAnimationEnd(Animator animator) {
               Log.e("txh","touch");
               allHit.add(hit);
               isHit = false;
               hit = 0;
               tv_hit.setText("0");
               ObjectAnimator alpha = ObjectAnimator.ofFloat(mode,"alpha",1,0).setDuration(600);
               alpha.start();
           }

           @Override
           public void onAnimationCancel(Animator animator) {

           }

           @Override
           public void onAnimationRepeat(Animator animator) {

           }
       });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout);
        Toast.makeText(this, "开始游戏", Toast.LENGTH_SHORT).show();
        re1 = findViewById(R.id.re1);
        re2 = findViewById(R.id.re2);
        re3 = findViewById(R.id.re3);
        re4 = findViewById(R.id.re4);
        tv_hit = findViewById(R.id.hit_click_num);
        tv_time = findViewById(R.id.time);
        tv_click_num = findViewById(R.id.click_num);
        Intent intent = getIntent();
        leveldown = intent.getIntExtra("leveldown",2500);
        leveltime = intent.getIntExtra("leveltime",500);
        WindowHeight = intent.getIntExtra("height",2000);
    }

    @Override
    protected void onResume() {
        super.onResume();
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                handler1.sendEmptyMessage(0);
            }
        }, 200, 1000);
        timer1 = new Timer();
        timer1.schedule(new TimerTask() {
            @Override
            public void run() {
                handler.sendEmptyMessage(0);
            }
        }, 0, leveltime);

    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);


    }
}
